﻿/*********************************************
 * User: Wangtao
 * Date: 13-3-1
 * Time: 上午11:16
 * 框架页面:负责整体框架结构的维护,中侧滚动条,右侧tab页面的管理,[消息框(待完善)]
 **********************************************/
var tabcontainer;//用以维护tabs头列表
$(function(){
      if(window!=top)
      {
          top.location.href=window.location.href;
      }
	  BindleftMenu();
      tabcontainer= new tabsmanager();
      frameResize();
      wtScrollBar.init();
	  $(window).resize(frameResize);
   //   initScroll();//初始化滚动条
    //  bindIframe();
})

function BindleftMenu(){   //此处是绑定左侧菜单
  $(".menucontent>ul>li").hover(
      function(){
         $(this).find("a:first").addClass("ontemp");
      },
	  function(){
         $(this).find("a:first").removeClass("ontemp");
   });

   $(".menucontent>ul>li>a").click(function(){
	   $(".menucontent>ul>li>a").siblings("ul").slideUp(300,function(){
           wtScrollBar.resetSize();
       }); //想显示的菜单收起

	   if($(this).siblings("ul").css("display")=="none"){
			  $(this).siblings("ul").slideDown(300,function(){
                  wtScrollBar.resetSize();
              });
	   }else{
			  $(this).siblings("ul").slideUp(300,function(){
                  wtScrollBar.resetSize();
              });
	   }//检测并操作子菜单的收放
	   return false;
	});
	
	$(".menucontent>ul>li>ul>li>a").click(function(){
	    $("a.on").removeClass("on");
		$(this).parent("li").parent("ul").siblings("a").addClass("on");
		$(".current").removeClass("current");
		$(this).addClass("current");
		//这里开始截断浏览器自定义转向,将所需操作移交给tab管理器,tabcontainer为全局管理器
		var name=$(this).text();
		var url= $(this).attr("href");
        if(url!=""&&url!=undefined) tabcontainer.addtab(name,url);
        return false;
		});
		
	$(".menucontent a").attr("hidefocus",true);//隐藏outline,主要为了兼顾ie6
}

function frameResize(){   //框架结构维护
   var ch=$(window).height()-$("#top_frame").outerHeight()-$("#buttom_frame").outerHeight()-2;
   $("#middel_frame").height(ch);
   //维护主内容页框架
   $("#c_left").height(ch);
   $("#c_middel").height(ch);
   $("#current_page").height(ch);
   //维护流动页框架
   var cw=$("#c_left:hidden").length==1?$(window).width()-$("#c_middel").outerWidth():$(window).width()-$("#c_left").outerWidth()-$("#c_middel").outerWidth();
   $("#current_page").width(cw);
   var chlidHeight= $("#current_page").height()-$(".tabs-head").outerHeight()-1;
   $(".tabs-panel").height(chlidHeight);
   $(".panel>iframe").height(chlidHeight);
    wtScrollBar.resetSize();  //重置滚动条
    if((typeof progress)!="undefined"&&progress.box) progress.changepostion();  //更改loading位置
}
//***************以下是右侧流动页面维护*******************/
function tab(){}     //这里是tab元素,包含标题和框架,名称和tab网址
tab.prototype={
    title:null,
    body:null,
    name:null,
    url:null
}

function tabsmanager(){           //tab管理器
    this.tablist=[];
    var temp = new tab();
    temp.title=$(".tabs-head li:eq(0)");
    temp.body=$(".tabs-panel div:eq(0)");
    temp=this.bindevent(temp);  //实例化管理器的同时创建一个tab
    wtScrollBar.bindIframe(temp.body.find("iframe"));   //将欢迎页绑定
    this.tablist.push(temp);   //将欢迎页面压入栈
    this.changetab(temp);
}

tabsmanager.prototype={
    constructor:tabsmanager,
    addtab:function(name,url){   //添加一个tab到管理器
        var temptab =this.createtab(name,url);
        temptab.title.appendTo(".tabs-head>ul");
        temptab.body.appendTo(".tabs-panel");
        temptab=this.bindevent(temptab);
        var existtab = this.isexits(temptab);
        if(existtab){
            this.deletetab(existtab);
        }
        this.tablist.push(temptab);
        this.changetab(temptab);
    },
    createtab:function(name,url){      //返回一个tab的实例
        var temptab = new tab();
        var tabhead='<li class="on">'+
            '<label class="title">'+name+'</label>'+
            '<a href="javascript:;" class="closeButton">×</a>'+
            '</li>';
        var tabbody='<div class="panel on">'+
            '<iframe scrolling="auto" frameborder="0" src="'+url+'" width="100%" height="100%"></iframe>'+
            '</div>';
        temptab.title=$(tabhead);
        temptab.body = $(tabbody);
        temptab.name=name;
        temptab.url=url;
        return temptab;
    },
    deletetab:function(tab){         //从管理器中删除一个tab
       for(var i=1;i<this.tablist.length;i++)
       {
           if(this.tablist[i]==tab)
           {
               this.tablist[i].title.remove();
               this.tablist[i].body.remove();
               if(this.tablist[i+1]!=undefined){
                   this.changetab(this.tablist[i+1]);
               }else{
                   this.changetab(this.tablist[i-1]);
               }
               this.tablist.splice(i,1);
           }
       }
    },
    deleteother:function(tab){        //这是对应方法,清除不等同于参数tab的其他tab
        for(var i=this.tablist.length-1;i>0;i--)
        {
            if(this.tablist[i]!=tab)
            {
                this.deletetab(this.tablist[i]);
            }
        }
    },
    deleteall:function(tab){        //这是对应方法,清空管理器,i>0标示排除欢迎页以外的页面
        for(var i=this.tablist.length-1;i>0;i--)
        {
            this.deletetab(this.tablist[i]);
        }
    },
    isexits:function(tab){          //检测一个tab是否存在
        for(var i=0;i<this.tablist.length;i++)
        {
            if(this.tablist[i].name==tab.name && this.tablist[i].url==tab.url)
            {
                return this.tablist[i];
            }
        }
        return false;
    },
    refreshtab:function(tab){        //刷新tab
        tab.body.find("iframe").attr("src",tab.url);
        this.changetab(tab);
    },
    changetab:function(tab){        //切换当前显示tab
       for(var i=0;i<this.tablist.length;i++)
       {
           if(this.tablist[i]==tab)
           {
               this.tablist[i].title.addClass("on");
               this.tablist[i].body.addClass("on");
           }else{
               this.tablist[i].title.removeClass("on");
               this.tablist[i].body.removeClass("on");
           }
       }
    },
    showrightmenu:function(tab,e){         //显示右键操作菜单
        var menu = '<div class="tabsrightmenu">'+
                    '<a class="rightmenu" id="refreshtab" href="javascript:;">刷新</a>'+
                    '<a class="rightmenu" id="closetab" href="javascript:;">关闭</a>'+
                    '<a class="rightmenu" id="closeother" href="javascript:;">关闭其他页面</a>'+
                    '<a class="rightmenu rightmenulast" id="closeall" href="javascript:;">关闭所有页面</a>'+
                    '</div>';
        menu=$(menu);
        var that=this;
        menu.find("#refreshtab").unbind("click").bind("click",function(){  //刷新
            menu.remove();

            tab.body.find("iframe").attr("src",tab.url.indexOf("?")==-1?tab.url+"?refresh="+Math.random():tab.url+"&refresh="+Math.random());
            return false;
        });
        menu.find("#closetab").unbind("click").bind("click",function(){  //关闭
            menu.remove();
            that.deletetab(tab);
            return false;
        });
        menu.find("#closeother").unbind("click").bind("click",function(){   //关闭其他
            menu.remove();
            that.deleteother(tab);
            return false;
        });
        menu.find("#closeall").unbind("click").bind("click",function(){   //关闭所有
            menu.remove();
            that.deleteall(tab);
            return false;
        });

        $(document).bind("mousedown",function(e){
            if(e.srcElement.className.indexOf("rightmenu")==-1){
                menu.remove();
            }
        });

        menu=menu.appendTo("body");
        menu.css({top:e.clientY+"px",left:e.clientX+"px","z-index":8003});
        menu.show();
    },
    bindevent:function(tab){       //给tab绑定全局事件
        var that=this;
        tab.title.find(".closeButton").unbind("click").bind("click",function(){  //绑定关闭按钮
            that.deletetab(tab);
            return false;
        });
        tab.title.find(".title").unbind("click").bind("click",function(e){   //绑定标签点击按钮
            that.changetab(tab);
            return false;
        });
        tab.title.unbind("contextmenu").bind("contextmenu",function(e){    //绑定右键菜单
            if(tab!=that.tablist[0]){
                that.showrightmenu(tab,e);
                return false;
            }
        });
        tab.title.find(".title").hover(function(){$(this).addClass("titlethover")},function(){$(this).removeClass("titlethover")});

        tab.body.find("iframe").load(function(){//绑定子页的一些事件
            try{
                wtScrollBar.bindIframe($(this));  //
                var thisTitle = $(this).contents().find("title").text();
                if(typeof(progress)!="undefined"&&progress.visible)
                {
                    progress.hide();
                }
                if(thisTitle!=name&&!thisTitle&&!thisTitle.trim())   //如果子页面标题与连接标题不同则以子标题为准
                {
                    tab.title.find(".title").text(thisTitle);
                }
            }catch(e){

            }
        });
        return tab;
    }
};
//***************以上是右侧流动页面维护*******************/
//***************滚动条代码开始*******************/
var wtScrollBar={
    dragMaker:false,
    _offsetTop:0,  //相对于顶部偏移
    _offsetHeight:0,   //相对于当前鼠标高度偏移
    middle:null,
    splitbar:null,
    scrollTool:null,
    scrollPanel:null,
    leftratio:0,
    middelratio:0,
    init:function(){
        this.scrollTool=$(".scrollT");
        this.scrollPanel=$(".scrollP");
        this.middle=$("#c_middel");
        this.splitbar =$(".framesetMiddel");
        this._offsetTop=this.middle.offset().top;
        this.resetSize();
        this.bindEvent();
    },
    resetSize:function(){
        if(this.scrollTool==null) this.init();
        this.leftratio=$("#c_left").height()/$(".menupanel").outerHeight();
        this.scrollTool.height(this.middle.height()*this.leftratio);
        this.scrollTool.css("top",($("#c_left").scrollTop()*this.leftratio)+"px");
        if(this.leftratio>=1)
        {
           this.hide();
        }else
        {
           this.show();
        }
    },
    show:function(){
        this.scrollTool.show();
        this.splitbar.hide();
    },
    hide:function(){
        this.scrollTool.hide();
        this.splitbar.show();
        this.splitbar.css("top",((this.middle.height()-this.splitbar.height())/2)+"px");
    },
    dragOff:function(e){
        this.dragMaker=false;
        this.middle.css("background","#E0DFDF");
        this.scrollTool.css("background","#168CAE");
    },
    dragOn:function(e){
        this.dragMaker=true;
        e=e||event;
        this._offsetHeight = e.offsetY;
        this.middle.css("background","#ccc");
        this.middle.css("cursor","default");
        this.scrollTool.css("background","#137C9B");
    },
    move:function(e){
        e=e||event;
        if (!this.dragMaker||e.which!=1) { return; }
        var top = e.clientY - this._offsetHeight - this._offsetTop;
        this.moveTo(top);
    },
    moveTo:function(y){
        var top = y;
        top = top < 0 ? 0 : top;
        if (top > this.middle.height() - this.scrollTool.height()) {
            top = this.middle.height() - this.scrollTool.height();
        }
        this.scrollTool.css("top", top + "px");
        $("#c_left").scrollTop(top/this.leftratio);
    },
    moveBy:function(option){
        var top;
        switch (option) {
            case "up":
                top = parseInt(this.scrollTool.css("top")) - this.scrollTool.height();
                break;
            case "down":
                top = parseInt(this.scrollTool.css("top")) + this.scrollTool.height();
                break;
        }
        this.moveTo(top);
    },
    childMove:function(e){
        e=e||event;
        if (!this.dragMaker||e.which!=1) { return; }
        var top = e.clientY - this._offsetHeight+$(".tabs-head").outerHeight();
        this.moveTo(top);
    },
    bindEvent:function(){
        var that=this;
        this.scrollTool.unbind("mousedown").bind("mousedown",function(e){
            that.dragOn(e);
        });

        this.scrollTool.unbind("mousemove").bind("mousemove",function(e){
            that.move(e);
        });

        this.scrollTool.unbind("mouseup").bind("mouseup",function(e){
            that.dragOff(e);
        });

        this.scrollTool.hover(function(){
            $(this).css("background","#137C9B");
        },function(){
            $(this).css("background","#168CAE");
        });

        $("body").unbind("mousemove").bind("mousemove",function(e){
            that.move(e);
        });
        $("body").unbind("selectstart").bind("selectstart", function(){return !that.dragMaker; });

        $("body").unbind("mouseup").bind("mouseup",function(e){//在页面任意地方弹起鼠标则滚动条关闭
            that.dragOff(e);
        });

        this.middle.click(function(e) {   //直接点击滚动槽时开判断操作
            e=e||event;
            if (e.clientY < that.scrollTool[0].getBoundingClientRect().top) {
                that.moveBy("up");
            }
            else if (e.clientY > that.scrollTool[0].getBoundingClientRect().bottom) {
                that.moveBy("down");
            }
        });

        $(".menupanel").bind("mousewheel",function(e){
            var y=e.originalEvent.wheelDelta|| e.wheelDelta;
            var top;
            if(y>0)
            {
                top = parseInt(that.scrollTool.css("top")) - (that.scrollTool.height()/5);
            }else{
                top = parseInt(that.scrollTool.css("top")) + (that.scrollTool.height()/5);
            }
            that.moveTo(top);
        });

        $(".controlLeft").toggle(function(){
            $("#c_left").hide();
            $(this).addClass("controlLefthover");
            frameResize();
        },function(){
            $("#c_left").show();
            $(this).removeClass("controlLefthover")
            frameResize();
        })
    },
    bindIframe:function(iframe){
        var that=this;
        try{
            iframe.contents().unbind("mousemove").bind("mousemove",function(e){
                that.childMove(e);
            });
            iframe.contents().unbind("mouseup").bind("mouseup",function(e){
                that.dragOff(e);
            });

        }catch(e){
            return;
        }
    }
};

var messagebox = {
    topindex:1000,
    show:function(message,type,autoclose){
        var msgbox='<div class="messagebox">'+
                    '<div class="icon"></div>'+
                    '<div class="content"></div>'+
                    '<a href="javascript:;" class="closeButton">×</a>'+
                    '</div>';
        msgbox=$(msgbox).prependTo("body");
        switch (type){
            case "alert":msgbox.addClass("msalert");break;
            case "ok":   msgbox.addClass("msok");break;
        }
        msgbox.find(".content").html(message);
        msgbox.css("z-index",this.topindex++);
        var top = ($(window).height()-msgbox.height())/2-msgbox.height();  //出现位置
        var stop= ($(window).height()-msgbox.height())/2;  //显示位置
        msgbox.css({"left":(($(window).width()-msgbox.width())/2)+"px","top":top});
        var contentTop=(msgbox.height()-msgbox.find(".content").height())/2-5;
        msgbox.find(".content").css("margin-top",(contentTop>=0?contentTop:0)+"px");
        msgbox.find(".closeButton").click(function(){
            msgbox.remove();
            return false;
        }).mousemove(function(){
            msgbox.stop();
        });

        msgbox.animate({top:stop+"px",opacity:"100"},500).animate({top:stop+"px"},3000,function(){
            if(autoclose){
                msgbox.animate({top:top+"px",opacity:"0"},500,function(){
                    msgbox.remove();
                });
            }
        });
    }
}

function cus_pass(message,autoClose){
    if(autoClose==undefined) autoClose=true;
    messagebox.show(message,"ok",autoClose);
}

function cus_alert(message,autoClose){
    if(autoClose==undefined) autoClose=true;
    messagebox.show(message,"alert",autoClose);
}



